/*
 * Created by JFormDesigner on Thu Apr 14 23:17:33 CST 2022
 */

package com.ZengQi.event.view;

import com.ZengQi.event.controller.AddCommentViewController;
import com.ZengQi.event.entity.Comment;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.GroupLayout;

/**
 * @author QI Zeng
 */
public class AddCommentView extends JFrame {
    private String commentContent;
    private String commentPublisher;
    private  String topicName;

    public AddCommentView(String commentPublisher,String topicName) {
        initComponents();
        this.commentContent=commentContentArea.getText();
        this.commentPublisher=commentPublisher;
        this.topicName=topicName;
        commentTxt.setText(topicName);
        setVisible(true);
        setLocationRelativeTo(null);


    }

    /**
     * 评论的思路是将评论的数据封装成一个comment对象然后添加到数据库里
     * @param e
     */
    private void commentBtn(ActionEvent e) {
        if(commentContentArea.getText()==null||"".equals(commentContentArea.getText().trim())){
            JOptionPane.showMessageDialog(this,"评论不能为空！");
            return;

        }
        Comment comment=new Comment();
        comment.setTopicName(topicName);
        comment.setCommentContent(commentContentArea.getText());
        comment.setCommentPublisher(commentPublisher);
        AddCommentViewController addCommentViewController=new AddCommentViewController();
        if(addCommentViewController.addSuccess(comment)){
            JOptionPane.showMessageDialog(this,"添加成功");
            dispose();


        }
    }
//组件布局及初始化
    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        panel1 = new JPanel();
        commentLabel = new JLabel();
        commentTxt = new JTextField();
        label1 = new JLabel();
        scrollPane1 = new JScrollPane();
        commentContentArea = new JTextArea();
        commentBtn = new JButton();

        //======== this ========
        setTitle("\u505a\u9898\u5e2e");
        setIconImage(new ImageIcon(getClass().getResource("/bang.png")).getImage());
        Container contentPane = getContentPane();

        //======== panel1 ========
        {

            //---- commentLabel ----
            commentLabel.setText("\u9898\u76ee\u540d\u79f0");

            //---- commentTxt ----
            commentTxt.setEditable(false);

            //---- label1 ----
            label1.setText("\u8bc4\u8bba");

            //======== scrollPane1 ========
            {
                scrollPane1.setViewportView(commentContentArea);
            }

            //---- commentBtn ----
            commentBtn.setText("\u8bc4\u8bba");
            commentBtn.addActionListener(e -> commentBtn(e));

            GroupLayout panel1Layout = new GroupLayout(panel1);
            panel1.setLayout(panel1Layout);
            panel1Layout.setHorizontalGroup(
                panel1Layout.createParallelGroup()
                    .addGroup(panel1Layout.createSequentialGroup()
                        .addGroup(panel1Layout.createParallelGroup()
                            .addGroup(panel1Layout.createSequentialGroup()
                                .addGap(82, 82, 82)
                                .addGroup(panel1Layout.createParallelGroup()
                                    .addGroup(panel1Layout.createSequentialGroup()
                                        .addComponent(label1, GroupLayout.PREFERRED_SIZE, 73, GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 409, GroupLayout.PREFERRED_SIZE))
                                    .addGroup(panel1Layout.createSequentialGroup()
                                        .addComponent(commentLabel, GroupLayout.PREFERRED_SIZE, 88, GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(commentTxt, GroupLayout.PREFERRED_SIZE, 311, GroupLayout.PREFERRED_SIZE))))
                            .addGroup(panel1Layout.createSequentialGroup()
                                .addGap(312, 312, 312)
                                .addComponent(commentBtn)))
                        .addContainerGap(236, Short.MAX_VALUE))
            );
            panel1Layout.setVerticalGroup(
                panel1Layout.createParallelGroup()
                    .addGroup(panel1Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(panel1Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(commentLabel, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
                            .addComponent(commentTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                        .addGap(27, 27, 27)
                        .addGroup(panel1Layout.createParallelGroup()
                            .addComponent(label1)
                            .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 128, GroupLayout.PREFERRED_SIZE))
                        .addGap(47, 47, 47)
                        .addComponent(commentBtn)
                        .addContainerGap(40, Short.MAX_VALUE))
            );
        }

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addComponent(panel1, GroupLayout.Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addComponent(panel1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JPanel panel1;
    private JLabel commentLabel;
    private JTextField commentTxt;
    private JLabel label1;
    private JScrollPane scrollPane1;
    private JTextArea commentContentArea;
    private JButton commentBtn;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
